<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 灏忓 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\news\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;
use function Qiniu\json_decode;

class AllController extends AdminBaseController
{
	
	public function list()
	{
		
		
		$param           = $this->request->param();
		$where=" a.delete_time =0 and a.parent_id!=0 ";
		$field="a.*";
		
		/***关键字*/
		$keywords=request()->param('keywords','','trim');
		if(!empty($keywords)){
			
			$where.=" and a.name like '%$keywords%' ";
		}
		
		/***上级*/
		$parent_id=request()->param('parent_id',0,'intval');
		if(!empty($parent_id)){
			
			$where.=" and a.parent_id = '$parent_id' ";
		}
		
		
		$listobj=db('dict a')
		->field($field)
		
		->where($where)->order('a.list_order desc,a.id desc')->paginate(10,false,['query'=>request()->param()]);
		
		
		$list=$listobj->items();
		
		$alllist=db('dict a')->where(['delete_time'=>0,'parent_id'=>0])->select()->toArray();
		
		$onelist=db('dict a')->where(['delete_time'=>0,'parent_id'=>0])->select()->column('name','id');
		
		
	
		$this->assign('list',$list);
		$this->assign('page',$listobj->render());
		
		$this->assign('alllist',$alllist);
		$this->assign('onelist',$onelist);
		
		$this->assign('parent_id', isset($param['parent_id']) ? $param['parent_id'] : '');
		$this->assign('keywords', isset($param['keywords']) ? $param['keywords'] : '');
		
		return $this->fetch();
		
	}
	public function edit()
	{
		
		$id = $this->request->param('id', 0, 'intval');
		
		$where=" a.id =$id ";
		$field="a.*";
		$post=db('dict a')
		->field($field)
		
		->where($where)
		->find();
		
		//p($post);
		
		$alllist=db('dict a')->where(['delete_time'=>0,'parent_id'=>0])->select()->toArray();
		$this->assign('alllist',$alllist);
		$this->assign('post', $post);
		
		
		return $this->fetch();
	}
	
	
	/**
	 * 缂栬緫椤圭洰鎻愪氦
	 * @adminMenu(
	 *     'name'   => '缂栬緫椤圭洰鎻愪氦',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '缂栬緫椤圭洰鎻愪氦',
	 *     'param'  => ''
	 * )
	 * @throws \think\Exception
	 */
	public function editPost()
	{
		
		if ($this->request->isPost()) {
			$data = $this->request->param();
			
			
			db('dict')->where(['id'=>$data['id']])->update($data);
			
			$this->success('修改成功!');
			
		}
	}
	public function add()
	{
	
		$alllist=db('dict a')->where(['delete_time'=>0,'parent_id'=>0])->select()->toArray();
		$this->assign('alllist',$alllist);
		return $this->fetch();
	}
	
	
	/**
	 * 缂栬緫椤圭洰鎻愪氦
	 * @adminMenu(
	 *     'name'   => '缂栬緫椤圭洰鎻愪氦',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '缂栬緫椤圭洰鎻愪氦',
	 *     'param'  => ''
	 * )
	 * @throws \think\Exception
	 */
	public function addPost()
	{
		
		if ($this->request->isPost()) {
			$data = $this->request->param();
			
			
			$id=db('dict')->insertGetId($data);
			
			$this->success('添加成功!',url('All/edit',['id'=>$id]));
			
		}
	}
	
	/* public function update()
	{
		$param           = $this->request->param();
		
		$status=$param['status'];
		
		if (isset($param['ids'])) {
			$ids = $this->request->param('ids/a');
			db('policys_join')->where('id', 'in', $ids)->update(['status' => $status]);
			foreach ($ids as $val){
				$this->add_need_log($val,1);
			}
			
			$this->success("操作成功", '');
		}
		
	} */
	
	public function delete()
	{
		$param           = $this->request->param();
		$portalPostModel = new PortalPostModel();
		
		if (isset($param['id'])) {
			$id           = $this->request->param('id', 0, 'intval');
			
			$resultPortal = db('dict')
			->where('id', $id)
			->update(['delete_time' =>time()]);
			if ($resultPortal) {				
				$this->success("删除成功", '');
			}
			
			
		}
		
		if (isset($param['ids'])) {
			$ids     = $this->request->param('ids/a');
			$result  = db('dict')->where('id', 'in', $ids)->update(['delete_time' => time()]);
			if ($result) {
				
				$this->success("批量删除成功", '');
			}
		}
	}
	public function logs()
	{
		
		
		$param           = $this->request->param();
		$where="";
		if(isset($param['id'])){
			$where.=" and a.join_id=".$param['id'];
		}
		$field="a.*";
		
		
		
		
		$listobj=db('needs_logs a')
		->field($field)
		
		->where($where)->order('a.id desc')->paginate(5,false,['query'=>request()->param()]);
		
		
		$list=$listobj->items();
		foreach ($list as &$val){
			$val['update_time']=date('Y-m-d',$val['update_time']);
			if(!empty($val['deal_users'])){
				$val['deal_users']=\json_decode($val['deal_users'],true);
			}else{
				$val['deal_users']=[];
			}
		}
		
		$this->assign('apply_type',apply_type());
		$this->assign('list',$list);
		$this->assign('total',$listobj->total());
		$this->assign('page',$listobj->render());
		return $this->fetch();
	}
	/**
	 * 椤圭洰鎺掑簭
	 * @adminMenu(
	 *     'name'   => '椤圭洰鎺掑簭',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鎺掑簭',
	 *     'param'  => ''
	 * )
	 */
	public function listOrder()
	{
		parent::listOrders(Db::name('dict'));
		$this->success("操作 成功", '');
	}
	
}